/*
 * Copyright (C) 2023 ab_skywalker@163.com All Rights Reserved.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
#ifndef __FOC_PARA_H
#define __FOC_PARA_H

#include <stdint.h>
#include <stdbool.h>

#define ANGLE2PAHSE(a) ((a) * 6280 / 360)

typedef enum 
{
    MOTOR_STATE_IDLE,
    MOTOR_STATE_POWERON,
    MOTOR_STATE_RUNNING,
    MOTOR_STATE_HALT,
    MOTOR_STATE_FAULT,
    MOTOR_STATE_MAX,
}MotorStateEnum;

typedef enum 
{
    MOTOR_WORK_TYPE_TORQUE,
    MOTOR_WORK_TYPE_VELOCITY,
    MOTOR_WORK_TYPE_POSITION,
    MOTOR_WORK_TYPE_MAX
}MotorWorkTypeEnmu;

typedef enum 
{
    MOTOR_ACC_TYPE_LINEAR,
    MOTOR_ACC_TYPE_SCURVE,
    MOTOR_ACC_TYPE_MAX,
}MotorAccTypeEnmu;

typedef struct 
{
    MotorStateEnum MotorState;
    MotorWorkTypeEnmu MotorWorkType;
    MotorAccTypeEnmu AccMode;//加速模式
    int8_t EncoderDir;//编码器方向 1：与电机同向；-1：与电机反向
    bool LocateEnabled;//定位模式
    int16_t EncoderOriginPos;//电角度为0时编码器的角度
    int16_t AccJerk;//s加速时加速度变化率
    int16_t TargetPhase;//目标角度
    int16_t TargetTorque;//目标力矩
    int16_t MaxTorque;//最大力矩
    int16_t TargetVelocity;//目标速度
    int16_t CurVelocity;//当前速度
    int16_t MaxVelocity;//最大速度
    int32_t TargetPosition;//目标位置
    int32_t TargetAcc;//目标加速度
    int32_t AccumulatePhase;
}MotorWorkParaStruct;

typedef struct 
{
    uint8_t PolePairs;//极对数
    uint8_t Resitance;//相电阻
    uint8_t Inductance;//相电感    
}MotorInherentParaStruct;

extern MotorWorkParaStruct MotorWorkPara;
extern MotorInherentParaStruct MotorInherentPara;

void motorParaInit(void);
void motorSetPhase(int16_t pahse);
void motorSetEncoterLocate(int8_t dir, int16_t pahse);
#endif //__FOC_PARA_H
